
import os
import threading
from concurrent.futures import ALL_COMPLETED, wait, Future
from typing import List
from case.execlHandle import ExcelHandle
from service import logger
from service.apiService import ApiService
from variable import handle_thread_pool

class ThreadHandleCase:

    def __init__(self):
        # self.api = ApiService()
        pass

    def multiple_file_handle(self, path) -> List[Future]:
        task_list: List[Future] = []

        files = os.listdir("./" + path)
        for file in files:
            thread_file = file
            if not os.path.isdir(thread_file) and thread_file.endswith(".xlsx"):
                t = handle_thread_pool.submit(self.case_task, path, thread_file)

                # t = threading.Thread(target=self.case_task, args=(path, thread_file,))
                # t.start()

                task_list.append(t)

        # for task in task_list:
        #     task.join()

        # wait(task_list, return_when=ALL_COMPLETED)
        handle_thread_pool.shutdown()

        return task_list

    def case_task(self, path, file):
        api = ApiService(path + "/" + file)
        for case in ExcelHandle(path + "/" + file).get_data():
            try:
                api.run_api(case)
            except AssertionError:
                pass

        if api.session is not None:
            api.session.close()

        if api.driver is not None:
            try:
                api.driver.close()
            except Exception as e:
                logger.debug(repr(e))



































































